//
//  ChatBarFaceView.m
//  keyboard
//
//  Created by 遇水 on 16/3/12.
//  Copyright © 2016年 遇水. All rights reserved.
//

#import "ChatBarFaceView.h"
#import "ChatBarFaceViewTopView.h"
#import "ChatBarFaceViewButtomView.h"
#import "UIView+Extension.h"
#import "FileManager.h"
#import "Utiles.h"
#import "FaceModel.h"
#import "MJExtension.h"


@interface ChatBarFaceView ()<ChatBarFaceViewButtomViewDelegate>

@property (nonatomic, strong) ChatBarFaceViewButtomView    *buttomView;

//懒加载的视图
@property (nonatomic, strong) ChatBarFaceViewTopView *defaultTopView;       //默认的
@property (nonatomic, strong) ChatBarFaceViewTopView *emojiTopView;      //emoji
@property (nonatomic, strong) ChatBarFaceViewTopView *LXHTopView;       //浪小花
@property (nonatomic, strong) ChatBarFaceViewTopView *NewlyTopView;     //最近
@property (nonatomic, strong) ChatBarFaceViewTopView *CustomTopView;        //自定义

@property (nonatomic, weak) ChatBarFaceViewTopView *showingTopView;     //当前显示的

@end


@implementation ChatBarFaceView
#pragma mark 懒加载


-(ChatBarFaceViewTopView *)defaultTopView{
    if(!_defaultTopView){
        self.defaultTopView = [[ChatBarFaceViewTopView alloc]init];
        //设置数据
        NSString *path = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/default/info.plist" ofType:nil];
        self.defaultTopView.dataSource = [FaceModel objectArrayWithKeyValuesArray:[NSArray arrayWithContentsOfFile:path]];
    }
    
    return _defaultTopView;
}

-(ChatBarFaceViewTopView *)emojiTopView{
    if(!_emojiTopView){
        self.emojiTopView = [[ChatBarFaceViewTopView alloc]init];
        //设置数据
                NSString *path = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/emoji/info.plist" ofType:nil];
                self.emojiTopView.dataSource = [FaceModel objectArrayWithKeyValuesArray:[NSArray arrayWithContentsOfFile:path]];
    }
    
    return _emojiTopView;
}


-(ChatBarFaceViewTopView *)LXHTopView{
    if(!_LXHTopView){
        self.LXHTopView = [[ChatBarFaceViewTopView alloc]init];
        //设置数据
                NSString *path = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/lxh/info.plist" ofType:nil];
                self.LXHTopView.dataSource = [FaceModel objectArrayWithKeyValuesArray:[NSArray arrayWithContentsOfFile:path]];
    }
    
    return _LXHTopView;
}

//此处不能采用懒架子啊的方式，如果采用懒加载那么数据就不会变怎么搞 呢
//-(ChatBarFaceViewTopView *)NewlyTopView{
//    if(!_NewlyTopView){
//        self.NewlyTopView = [[ChatBarFaceViewTopView alloc]init];
////        self.NewlyTopView.backgroundColor  = [UIColor blueColor];
//        self.NewlyTopView.isCustomView = YES;
//        //设置数据
//                NSString *path = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/newly/info.plist" ofType:nil];
//                self.NewlyTopView.dataSource = [FaceModel objectArrayWithKeyValuesArray:[NSArray arrayWithContentsOfFile:path]];
//    }
//    return _NewlyTopView;
//}

//自定义的    此处需要自己实现不同的样式
//-(ChatBarFaceViewTopView *)CustomTopView{
//    if(!_CustomTopView){
//        self.CustomTopView = [[ChatBarFaceViewTopView alloc]init];
//        self.CustomTopView.isCustomView = YES;
//        //设置数据
//                NSString *path = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/custom/info.plist" ofType:nil];
//        
//                self.CustomTopView.dataSource = [FaceModel objectArrayWithKeyValuesArray:[NSArray arrayWithContentsOfFile:path]];
//    }
//    
//    return _CustomTopView;
//}




-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubview];
    }
    
    return self;
}

-(void)initSubview
{
    ChatBarFaceViewButtomView *buttomView =[[ChatBarFaceViewButtomView alloc]init];
    buttomView.delegate = self;
    [self addSubview:buttomView];
    self.buttomView = buttomView;
}



-(void)layoutSubviews
{
    [super layoutSubviews];
    //进行子控件布局
    self.buttomView.x = 0;

    self.buttomView.width = self.width;
    self.buttomView.height = 35;
    self.buttomView.y = self.height - self.buttomView.height;

    //上面的视图实时在更换，并且保留了当前正在显示的视图，所以只需要设置当前正在显示的视图的frame就好了
    self.showingTopView.x = 0;
    self.showingTopView.y = 0 ;
    self.showingTopView.height = self.buttomView.y;
    self.showingTopView.width = self.width;
    
}



-(void)chatBarFaceViewButtomView:(ChatBarFaceViewButtomView *)tabBar didSelectButton:(FaceViewButtomViewButtonType)buttonType
{
    //首先先将当前显示的移除
    [self.showingTopView removeFromSuperview];
    
    switch(buttonType) {
        case FaceViewButtomViewButtonTypeDefault: {
            //将对应的而视图添加进去,并设置为当前显示视图
            [self addSubview:self.defaultTopView];
            self.showingTopView = self.defaultTopView;
            break;
        }
        case FaceViewButtomViewButtonTypeEmoji: {
            [self addSubview:self.emojiTopView];
            self.showingTopView = self.emojiTopView;
            break;
        }
        case FaceViewButtomViewButtonTypeLXH: {
            [self addSubview:self.LXHTopView];
            self.showingTopView = self.LXHTopView;
            break;
        }
        case FaceViewButtomViewButtonTypeNewly: {
            //创建控件并且设置
            ChatBarFaceViewTopView *NewlyTopView = [[ChatBarFaceViewTopView alloc]init];
            //设置数据
            NSString *path = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/newly/info.plist" ofType:nil];
            self.NewlyTopView.dataSource = [FaceModel objectArrayWithKeyValuesArray:[NSArray arrayWithContentsOfFile:path]];

            [self addSubview:self.NewlyTopView = NewlyTopView];
            self.showingTopView = self.NewlyTopView;
            break;
        }
        case FaceViewButtomViewButtonTypeCustom: {
            self.CustomTopView = [[ChatBarFaceViewTopView alloc]init];
            self.CustomTopView.isCustomView = YES;
                    //设置数据
            NSString *path = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/custom/info.plist" ofType:nil];
            
            NSMutableArray *array =(NSMutableArray *) [FaceModel objectArrayWithKeyValuesArray:[NSArray arrayWithContentsOfFile:path]];
            if (array.count == 0 || array ==nil) {
                FaceModel *model = [[FaceModel alloc]init];
                model.png = @"faceAdd";
                [array addObject:model];
            }
            
            self.CustomTopView.dataSource = array;
            [self addSubview:self.CustomTopView];
            self.showingTopView = self.CustomTopView;
            break;
        }
            
        case FaceViewButtomViewButtonTypeSend:{
            break;
        }
            
            
    }
    //刷新布局
    [self setNeedsLayout];
}

/**
 *  点击的是发送按钮， 此处的问题是如何获得textView中的内容

 */
-(void)chatBarFaceViewButtomView:(ChatBarFaceViewButtomView *)tabBar didSelectButtonSend:(NSString *)sendString
{
    //发送一个通知   ，，告诉chatBar发送消息, 
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationSendMessage object:nil];
}



@end
